package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class DelOperation implements IOperation{
    @Override
    public void work(BookList booklist) {
        System.out.println("删除图书...");
        System.out.println("请输入你要删除的图书的书名：");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();

        int currentSize = booklist.getUsedSize();

        int index = -1;
        //遍历书架
        int i = 0;
        for (; i < currentSize; i++) {
            Book book = booklist.getBook(i);
            if(name.equals(book.getName())) {
                index = i;
                break;
            }
        }
        if(i >= currentSize) {
            System.out.println("没有要删除的图书...");
            return;
        }
        for (int j = index; j < currentSize-1; j++) {
            Book book = booklist.getBook(j+1);
            booklist.setBook(j,book);
        }
        booklist.setUsedSize(currentSize-1);
        System.out.println("删除图书成功！！！");
    }
}
